<!DOCTYPE HTML>
<html>
<head>
  <title>Test Encrypted Media Extensions</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="manifest.js"></script>
  <script type="text/javascript" src="http://test1.mochi.test:8888/tests/dom/media/test/eme.js"></script>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
var manager = new MediaTestManager;

function ArrayBuffersEqual(a, b) {
  if (a.byteLength != b.byteLength) {
    return false;
  }
  var ua = new Uint8Array(a);
  var ub = new Uint8Array(b);
  for (var i = 0; i < ua.length; i++) {
    if (ua[i] != ub[i]) {
      return false;
    }
  }
  return true;
}

function KeysChangeFunc(session, keys, token) {
  session.keyIdsReceived = [];
  for (var keyid in keys) {
    Log(token, "Set " + keyid + " to false in session[" + session.sessionId + "].keyIdsReceived");
    session.keyIdsReceived[keyid] = false;
  }
  return function(ev) {
    var session = ev.target;
    session.gotKeysChanged = true;

    var keyList = [];
    var valueList = [];
    var map = session.keyStatuses;

    // Test that accessing keys not known to the CDM has expected behaviour.
    var absentKey = new Uint8Array([0xeb, 0xdd, 0x62, 0xf1, 0x68, 0x14, 0xd2, 0x7b,
                                    0x68, 0xef, 0x12, 0x2a, 0xfc, 0xe4, 0xae, 0x3c]);
    is(map.has(absentKey), false, "Shouldn't have a key that's not in the media");
    is(map.get(absentKey), undefined, "Unknown keys should undefined status");

    // Verify known keys have expected status.
    for (var [key, val] of map.entries()) {
      is(key.constructor, ArrayBuffer, "keyId should be ArrayBuffer");
      ok(map.has(key), "MediaKeyStatusMap.has() should work.");
      is(map.get(key), val, "MediaKeyStatusMap.get() should work.");
      keyList.push(key);
      valueList.push(val);
      is(val, "usable", token + ": key status should be usable");
      var kid = Base64ToHex(window.btoa(ArrayBufferToString(key)));
      ok(kid in session.keyIdsReceived, TimeStamp(token) + " session[" + session.sessionId + "].keyIdsReceived contained " + kid + " as expected.");
      session.keyIdsReceived[kid] = true;
    }

    var index = 0;
    for (var keyId of map.keys()) {
      ok(ArrayBuffersEqual(keyId, keyList[index]), "MediaKeyStatusMap.keys() should correspond to entries");
      index++;
    }
    index = 0;
    for (var val of map.values()) {
      is(val, valueList[index], "MediaKeyStatusMap.values() should correspond to entries");
      index++;
    }
  }
}

function startTest(test, token)
{
  manager.started(token);

  var sessions = [];

  var v = SetupEME(test, token,
    {
      onsessioncreated: function(session) {
        sessions.push(session);
        session.addEventListener("keystatuseschange", KeysChangeFunc(session, test.keys, token), false);

        session.numKeystatuseschangeEvents = 0;
        session.numOnkeystatuseschangeEvents = 0;

        session.addEventListener("keystatuseschange", function() {
          session.numKeystatuseschangeEvents += 1;
        });
        session.onkeystatuseschange = function() {
          session.numOnkeystatuseschangeEvents += 1;
        };

        session.numMessageEvents = 0;
        session.numOnMessageEvents = 0;
        session.addEventListener("message", function() {
          session.numMessageEvents += 1;
        });
        session.onmessage = function() {
          session.numOnMessageEvents += 1;
        };
      }
    }
  );

  document.body.appendChild(v);

  var gotEncrypted = 0;

  v.addEventListener("encrypted", function(ev) {
    gotEncrypted += 1;
  });

  v.addEventListener("loadedmetadata", function() {
    ok(SpecialPowers.do_lookupGetter(v, "isEncrypted").apply(v),
       TimeStamp(token) + " isEncrypted should be true");
    is(v.isEncrypted, undefined, "isEncrypted should not be accessible from content");
  });

  v.addEventListener("ended", function(ev) {
    ok(true, TimeStamp(token) + " got ended event");

    is(gotEncrypted, test.sessionCount,
       TimeStamp(token) + " encrypted events expected: " + test.sessionCount
       + ", actual: " + gotEncrypted);

    ok(Math.abs(test.duration - v.duration) < 0.1,
       TimeStamp(token) + " Duration of video should be corrrect");
    ok(Math.abs(test.duration - v.currentTime) < 0.1,
       TimeStamp(token) + " Current time should be same as duration");

    // Verify all sessions had all keys went sent to the CDM usable, and thus
    // that we received keystatuseschange event(s).
    is(sessions.length, test.sessionCount, TimeStamp(token) + " should have "
                        + test.sessionCount
                        + " session" + (test.sessionCount === 1 ? "" : "s"));
    var keyIdsReceived = [];
    for (var keyid in test.keys) { keyIdsReceived[keyid] = false; }
    for (var i = 0; i < sessions.length; i++) {
      var session = sessions[i];
      ok(session.gotKeysChanged,
         TimeStamp(token) + " session[" + session.sessionId
         + "] should have received at least one keychange event");
      for (var kid in session.keyIdsReceived) {
        Log(token, "session[" + session.sessionId + "] key " + kid + " = " + (session.keyIdsReceived[kid] ? "true" : "false"));
        if (session.keyIdsReceived[kid]) { keyIdsReceived[kid] = true; }
      }
      ok(session.numKeystatuseschangeEvents > 0, TimeStamp(token) + " should get key status changes");
      is(session.numKeystatuseschangeEvents, session.numOnkeystatuseschangeEvents,
         TimeStamp(token) + " should have as many keystatuseschange event listener calls as event handler calls.");

      ok(session.numMessageEvents > 0, TimeStamp(token) + " should get message events");
      is(session.numMessageEvents, session.numOnMessageEvents,
         TimeStamp(token) + " should have as many message event listener calls as event handler calls.");
    }
    for (var kid in keyIdsReceived) {
      ok(keyIdsReceived[kid], TimeStamp(token) + " key with id " + kid + " was usable as expected");
    }

    v.closeSessions().then(() => manager.finished(token));
  });

  LoadTest(test, v, token)
  .then(function() {
    v.play();
  }).catch(function() {
    ok(false, token + " failed to load");
    manager.finished(token);
  });
}

function beginTest() {
  manager.runTests(gEMETests, startTest);
}

if (!IsMacOSSnowLeopardOrEarlier()) {
  SimpleTest.waitForExplicitFinish();
  SetupEMEPref(beginTest);
} else {
  todo(false, "Test disabled on this platform.");
}
</script>
</pre>
</body>
</html>
